package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.PropertiesUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

/**
 * Created by zuoweiguang on 2016/9/6.
 */
public class ActualTimeTraffic extends ActualTimeRequest {

    private Logger logger = Logger.getLogger(ActualTimeTraffic.class);
    private DBCollection corresCol;
    private DBCollection trafficStatusCol;

    public void refrashTraffic() {
        int rtic_count = 0;
        try {
            String url = propObj.getString("RticTraffic");
            String result = sendGet(url);
//            String result = readRticFile();

            JSONObject resultObj = JSONObject.parseObject(result);
            JSONObject resultObjJSONObject = resultObj.getJSONObject("result");
            String timestamp = resultObjJSONObject.getString("timestamp");

            List<JSONObject> rticList = (List<JSONObject>) resultObjJSONObject.get("rtic");

            corresCol = getMongoCollection("corresponding");
            trafficStatusCol = getMongoCollection("traffic_status");

            for (JSONObject rticJson: rticList) {
                try {
                    String rtic_id = rticJson.getString("id");
                    String mesh_no = rticJson.getString("mesh");
                    String rtic_kind = rticJson.getString("kind");
                    String rtic_length = rticJson.getString("length");
                    JSONObject flow = rticJson.getJSONObject("flow");
                    String los = flow.getString("los");
                    String traveltime = flow.getString("traveltime");

                    //拼接rticId
                    while (rtic_id.length() != 4) {
                        rtic_id = "0" + rtic_id;
                    }
                    rtic_id = mesh_no + rtic_kind + rtic_id;

                    //查询 correspondingOfxian 对应的link_pid
                    BasicDBObject query = new BasicDBObject();
                    query.append("mesh_code", Integer.valueOf(mesh_no));
                    query.append("rtic_id", rtic_id);
                    query.append("rtic_link_kind", Integer.valueOf(rtic_kind));
                    DBObject rtic_info = trafficStatusCol.findOne(query);
//                    System.out.println("query:" + query.toString());
                    if (rtic_info != null) {
                        try {
                            int old_status = (Integer) rtic_info.get("status");

                            DBObject trafficObj = new BasicDBObject();
                            int status = Integer.valueOf(los);
                            trafficObj.put("status", status);
                            trafficObj.put("temp_status", old_status);
                            trafficObj.put("traveltime", Integer.valueOf(traveltime));
                            trafficObj.put("updatetime", sdf.format(new Date()));
                            trafficObj.put("rtic_updatetime", timestamp);

                            //更新当前link路况到mongo
                            trafficStatusCol.update(query, new BasicDBObject("$set", trafficObj), false, true);
//                            System.out.println("update:" + trafficObj.toString());
                            rtic_count += 1;
                            if (rtic_count % 2000 == 0) {
                                System.out.println("rtic count:" + rtic_count);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("refrash rtic total:" + rtic_count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeMongo();
        }

    }

    public static String readRticFile(){
        String rootPath = PropertiesUtil.class.getClassLoader().getResource("").getPath();
        String filePath = rootPath + "/RTICTraffic.json";
        StringBuffer result = new StringBuffer();
        try {
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
//                    System.out.println(lineTxt);
                    result.append(lineTxt);
                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        } finally {
            System.out.println(result.toString());
            return result.toString();
        }

    }


    public static void main(String[] args) {
//        String result = readTxtFile();
        new ActualTimeTraffic().refrashTraffic();
    }

}